package sqba.jamper.util;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class XMLNmsManager{
	static DefaultTableModel model;
	static JTable table;
	{
		String t[] = {"Namespace","URI"};
		model = new DefaultTableModel(t,0);
		table = new JTable(model);
	}
	
	public class AddActionListener implements ActionListener{
		 JTable table;
		 
		 AddActionListener(JTable table){
		  this.table = table;
		 }
		 
		 public void actionPerformed(ActionEvent e){
			 DefaultTableModel model = (DefaultTableModel) table.getModel();
			 String arr[] = {"NewNameSpace", "URI"};
			 model.addRow(arr);
		 }
	}
	public class RemoveActionListener implements ActionListener{
		 JTable table;
		 RemoveActionListener(JTable table){
		  this.table = table;
		 }
		 
		 public void actionPerformed(ActionEvent e){
		  int row = table.getSelectedRow();
		  if ( row == -1)
		   return;
		  DefaultTableModel model = (DefaultTableModel) table.getModel();
		  model.removeRow(row);
		 }
	}
	
	public XMLNmsManager(JPanel pan){
        JPanel panel = new JPanel();
        JButton button1 = new JButton("add");
        JButton button2 = new JButton("del");
        panel.add(button1);
        panel.add(button2);
        button1.addActionListener(new AddActionListener(table));
        button2.addActionListener(new RemoveActionListener(table));
        
        pan.setLayout(new BorderLayout());
        //Create the scroll pane and add the table to it.
        //JScrollPane scrollPane = new JScrollPane(table);
        pan.add(new JScrollPane(table),BorderLayout.CENTER);
        //Add the scroll pane to this panel.
        pan.add(panel,BorderLayout.SOUTH);
	}
	
	public static Vector getDataVector(){
		return model.getDataVector();
	}
	
	public static String getDataXML(){
		String ret = "<namespaces>";
		Vector namespaces = XMLNmsManager.getDataVector();
		for( int i = 0 ; i <namespaces.size() ; i++ ){
			ret += "<namespace><name>"+(String)((Vector)namespaces.elementAt(i)).elementAt(0);
			ret += "</name><uri>"+(String)((Vector)namespaces.elementAt(i)).elementAt(1);
			ret += "</uri></namespace>";
		}
		ret += "</namespaces>";
		return ret;
	}
	
	public static void readFile(String uri){
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder;
			builder = factory.newDocumentBuilder();
			Document doc = builder.parse(uri);
			NodeList list = doc.getElementsByTagName("namespace");
			for( int i = 0 ; i < list.getLength() ; i++ ){
				Node t = list.item(i);
				NodeList tt = t.getChildNodes();
				//for( int j = 0 ; j < tt.getLength() ; j++ )
				//	System.out.println( j + " " + tt.item(j).getNodeName() + " "  + tt.item(j).getTextContent() );
				Object newitem[] = {tt.item(0).getTextContent(), tt.item(1).getTextContent()};
				model.addRow(newitem);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("SimpleTableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        
        JPanel newContentPane = new JPanel();
        XMLNmsManager xm = new XMLNmsManager(newContentPane);
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
